﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace SkiPathFinder
{
    public class GraphForm : Form
    {
        public GraphForm()
        {
            InitializeComponent();

            //populate the list of available ski paths
            PopulatePathList();
        }

        

        private ZedGraph.ZedGraphControl graphControl;
        private Label lblSelectPath;
        private ComboBox cmbxPathList;
        private Button btnClose;
        private Label lblDistance;
        private Label label1;
        private Label lblUphill;
        private Label lblDownhill;
        private Panel panel1;
        private Label lblNormalUphillElevationGain;
        private Label lblNormalDownhillElevationGain;
        private Label lblHighUphillElevationGain;
        private Label lblHighDownhillElevationGain;

        private string _selectedPathName = string.Empty;

       

        /// <summary>
        /// The path which is drawn in the graph when the form is shown 
        /// </summary>
        public string SelectedPathName
        {
            get { return _selectedPathName; }
            set 
            {
                _selectedPathName = value;
                cmbxPathList.SelectedValue = _selectedPathName;     
            }
        }

        private void PopulatePathList()
        {
            cmbxPathList.Items.Clear();
            
            string[] shapefiles = Directory.GetFiles(Globals.SelectedAreaFolder, "*.shp");
            foreach (string fileName in shapefiles)
            {
                cmbxPathList.Items.Add(Path.GetFileNameWithoutExtension(fileName));
            }
        }

        /// <summary>
        /// When the user selects a new path
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbxPathList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbxPathList.SelectedIndex >= 0)
            {
                DrawGraph(cmbxPathList.SelectedItem.ToString());
            }
        }

        /// <summary>
        /// Draws the graph of the ski path
        /// </summary>
        /// <param name="p"></param>
        private void DrawGraph(string pathName)
        {
            //get the elevation profile table
            string csvFileName = Path.Combine(Globals.SelectedAreaFolder, pathName + "-profile.csv");

            if (File.Exists(csvFileName))
            {
                DataTable table = CsvFileParser.ParseFileToDataTable(csvFileName, true);

                //get the distance and elevation arrays..
                int numRows = table.Rows.Count;
                double[] distanceArray = new double[numRows];
                double[] elevationArray = new double[numRows];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    distanceArray[i] = Convert.ToDouble(table.Rows[i][0].ToString());
                    elevationArray[i] = Convert.ToDouble(table.Rows[i][1].ToString());
                }
               
                graphControl.GraphPane.CurveList.Clear();

                ZedGraph.LineItem myCurve = graphControl.GraphPane.AddCurve("Elevation Profile", distanceArray, elevationArray, Color.Blue);
                myCurve.Line.Width = 2.0f;
                myCurve.Symbol.Type = ZedGraph.SymbolType.None;
                myCurve.Line.Fill.Color = Color.LightBlue;
                myCurve.Line.Fill.Color = Color.FromArgb(100, 0, 0, 255); 
                myCurve.Line.Fill.IsVisible = true;

                graphControl.GraphPane.XAxis.Title.Text = "Distance (meters)";
                graphControl.GraphPane.YAxis.Title.Text = "Elevation (meters)";
                graphControl.AxisChange();

                //set the graph title
                graphControl.GraphPane.Title.Text = cmbxPathList.SelectedItem.ToString();

                

                //set the grid
                graphControl.GraphPane.XAxis.MajorGrid.Color = Color.Gray;
                graphControl.GraphPane.XAxis.MajorGrid.DashOn = 2f;
                graphControl.GraphPane.XAxis.MajorGrid.PenWidth = 1f;
                graphControl.GraphPane.XAxis.MajorGrid.IsVisible = true;

                graphControl.GraphPane.YAxis.MajorGrid.Color = Color.Gray;
                graphControl.GraphPane.YAxis.MajorGrid.DashOn = 2f;
                graphControl.GraphPane.YAxis.MajorGrid.PenWidth = 1f;
                graphControl.GraphPane.YAxis.MajorGrid.IsVisible = true;

                graphControl.Refresh();
                graphControl.AxisChange();

                //set other information
                PathAnalyzer analyzer = new PathAnalyzer();
                DataTable pathInfoTable = analyzer.AnalyzePath(table);
                DataTable riskValue = analyzer.AnalyzePathRisk(65.0,45.0);
                lblDistance.Text = "Distance: " + pathInfoTable.Rows[0][0].ToString() + " meters";
                lblUphill.Text = "Uphill Elevation Gain: " + pathInfoTable.Rows[0][1].ToString() + " meters";
                lblDownhill.Text = "Downhill Elevation Gain: " + pathInfoTable.Rows[0][2].ToString() + " meters";
                lblHighDownhillElevationGain.Text = "HighDownHillElevationGain:" + riskValue.Rows[0][2].ToString() + " meters";
                lblNormalDownhillElevationGain.Text = "NormalDownHillElevationGain:" + riskValue.Rows[0][3].ToString() + " meters";
                lblHighUphillElevationGain.Text = "HighUpHillElevationGain:" + riskValue.Rows[0][0].ToString() + " meters";
                lblNormalUphillElevationGain.Text = "NormalUpHillElevationGain:" + riskValue.Rows[0][1].ToString() + " meters";

            }
            else
            {
                //if csv file does not exist, it needs to be recalculated
            }
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

       

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GraphForm));
            this.graphControl = new ZedGraph.ZedGraphControl();
            this.lblSelectPath = new System.Windows.Forms.Label();
            this.cmbxPathList = new System.Windows.Forms.ComboBox();
            this.btnClose = new System.Windows.Forms.Button();
            this.lblDistance = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.lblUphill = new System.Windows.Forms.Label();
            this.lblDownhill = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.lblNormalUphillElevationGain = new System.Windows.Forms.Label();
            this.lblNormalDownhillElevationGain = new System.Windows.Forms.Label();
            this.lblHighUphillElevationGain = new System.Windows.Forms.Label();
            this.lblHighDownhillElevationGain = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // graphControl
            // 
            this.graphControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.graphControl.Location = new System.Drawing.Point(12, 44);
            this.graphControl.Name = "graphControl";
            this.graphControl.ScrollGrace = 0;
            this.graphControl.ScrollMaxX = 0;
            this.graphControl.ScrollMaxY = 0;
            this.graphControl.ScrollMaxY2 = 0;
            this.graphControl.ScrollMinX = 0;
            this.graphControl.ScrollMinY = 0;
            this.graphControl.ScrollMinY2 = 0;
            this.graphControl.Size = new System.Drawing.Size(461, 319);
            this.graphControl.TabIndex = 0;
            // 
            // lblSelectPath
            // 
            this.lblSelectPath.AutoSize = true;
            this.lblSelectPath.Location = new System.Drawing.Point(12, 19);
            this.lblSelectPath.Name = "lblSelectPath";
            this.lblSelectPath.Size = new System.Drawing.Size(125, 13);
            this.lblSelectPath.TabIndex = 1;
            this.lblSelectPath.Text = "Please select a Ski Path:";
            // 
            // cmbxPathList
            // 
            this.cmbxPathList.FormattingEnabled = true;
            this.cmbxPathList.Location = new System.Drawing.Point(143, 16);
            this.cmbxPathList.Name = "cmbxPathList";
            this.cmbxPathList.Size = new System.Drawing.Size(171, 21);
            this.cmbxPathList.TabIndex = 2;
            this.cmbxPathList.SelectedIndexChanged += new System.EventHandler(this.cmbxPathList_SelectedIndexChanged);
            // 
            // btnClose
            // 
            this.btnClose.Location = new System.Drawing.Point(379, 503);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(104, 28);
            this.btnClose.TabIndex = 3;
            this.btnClose.Text = "Close";
            this.btnClose.UseVisualStyleBackColor = true;
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // lblDistance
            // 
            this.lblDistance.AutoSize = true;
            this.lblDistance.Location = new System.Drawing.Point(3, 8);
            this.lblDistance.Name = "lblDistance";
            this.lblDistance.Size = new System.Drawing.Size(55, 13);
            this.lblDistance.TabIndex = 5;
            this.lblDistance.Text = "Distance: ";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(17, 375);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(138, 16);
            this.label1.TabIndex = 4;
            this.label1.Text = "Ski Path Information";
            // 
            // lblUphill
            // 
            this.lblUphill.AutoSize = true;
            this.lblUphill.Location = new System.Drawing.Point(3, 25);
            this.lblUphill.Name = "lblUphill";
            this.lblUphill.Size = new System.Drawing.Size(111, 13);
            this.lblUphill.TabIndex = 6;
            this.lblUphill.Text = "Uphill Elevation Gain: ";
            // 
            // lblDownhill
            // 
            this.lblDownhill.AutoSize = true;
            this.lblDownhill.Location = new System.Drawing.Point(3, 42);
            this.lblDownhill.Name = "lblDownhill";
            this.lblDownhill.Size = new System.Drawing.Size(122, 13);
            this.lblDownhill.TabIndex = 7;
            this.lblDownhill.Text = "Downhill Elevation Gain:";
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.lblNormalUphillElevationGain);
            this.panel1.Controls.Add(this.lblNormalDownhillElevationGain);
            this.panel1.Controls.Add(this.lblHighUphillElevationGain);
            this.panel1.Controls.Add(this.lblHighDownhillElevationGain);
            this.panel1.Controls.Add(this.lblDownhill);
            this.panel1.Controls.Add(this.lblDistance);
            this.panel1.Controls.Add(this.lblUphill);
            this.panel1.Location = new System.Drawing.Point(12, 400);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(211, 137);
            this.panel1.TabIndex = 8;
            // 
            // lblNormalUphillElevationGain
            // 
            this.lblNormalUphillElevationGain.AutoSize = true;
            this.lblNormalUphillElevationGain.ForeColor = System.Drawing.Color.Blue;
            this.lblNormalUphillElevationGain.Location = new System.Drawing.Point(4, 116);
            this.lblNormalUphillElevationGain.Name = "lblNormalUphillElevationGain";
            this.lblNormalUphillElevationGain.Size = new System.Drawing.Size(141, 13);
            this.lblNormalUphillElevationGain.TabIndex = 7;
            this.lblNormalUphillElevationGain.Text = "NormalUphill Elevation Gain:";
            // 
            // lblNormalDownhillElevationGain
            // 
            this.lblNormalDownhillElevationGain.AutoSize = true;
            this.lblNormalDownhillElevationGain.ForeColor = System.Drawing.Color.Blue;
            this.lblNormalDownhillElevationGain.Location = new System.Drawing.Point(4, 96);
            this.lblNormalDownhillElevationGain.Name = "lblNormalDownhillElevationGain";
            this.lblNormalDownhillElevationGain.Size = new System.Drawing.Size(158, 13);
            this.lblNormalDownhillElevationGain.TabIndex = 7;
            this.lblNormalDownhillElevationGain.Text = "Normal Downhill Elevation Gain:";
            // 
            // lblHighUphillElevationGain
            // 
            this.lblHighUphillElevationGain.AutoSize = true;
            this.lblHighUphillElevationGain.ForeColor = System.Drawing.Color.Red;
            this.lblHighUphillElevationGain.Location = new System.Drawing.Point(3, 77);
            this.lblHighUphillElevationGain.Name = "lblHighUphillElevationGain";
            this.lblHighUphillElevationGain.Size = new System.Drawing.Size(130, 13);
            this.lblHighUphillElevationGain.TabIndex = 7;
            this.lblHighUphillElevationGain.Text = "HighUphill Elevation Gain:";
            // 
            // lblHighDownhillElevationGain
            // 
            this.lblHighDownhillElevationGain.AutoSize = true;
            this.lblHighDownhillElevationGain.ForeColor = System.Drawing.Color.Red;
            this.lblHighDownhillElevationGain.Location = new System.Drawing.Point(3, 60);
            this.lblHighDownhillElevationGain.Name = "lblHighDownhillElevationGain";
            this.lblHighDownhillElevationGain.Size = new System.Drawing.Size(144, 13);
            this.lblHighDownhillElevationGain.TabIndex = 7;
            this.lblHighDownhillElevationGain.Text = "HighDownhill Elevation Gain:";
            // 
            // GraphForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(495, 543);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.cmbxPathList);
            this.Controls.Add(this.lblSelectPath);
            this.Controls.Add(this.graphControl);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "GraphForm";
            this.Text = "Elevation Profile Graph";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
